package com.tangsm.spring.boot.filter.controller;

import com.tangsm.spring.boot.filter.common.listener.MyHttpSessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 鉴定器验证接口
 *
 * @author tangsm
 */
@RestController
public class ListenerController {
    private static final Logger log = LoggerFactory.getLogger(ListenerController.class);

    /**
     * 登录
     *
     * @param request 请求信息
     * @return 响应信息
     */
    @GetMapping("addSession")
    public String addSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("name", "haixiang");
        return "当前在线人数" + session.getServletContext().getAttribute("sessionCount") + "人";
    }

    /**
     * 登出
     *
     * @param request 请求信息
     * @return 响应信息
     */
    @GetMapping("removeSession")
    public String removeSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "当前在线人数" + session.getServletContext().getAttribute("sessionCount") + "人";
    }

    /**
     * 查询当前在线人数
     *
     * @return 响应信息
     */
    @GetMapping("online")
    public String online() {
        int online = MyHttpSessionListener.userCount.get();
        log.info("当前在线人数：{}", online);
        return "当前在线人数" + online;
    }
}
